<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<pre id="pre">
			
		</pre>
		<script>
			const url = "https://mock.presstime.cn/mock/64acf0fe9bf1f33e6d0bb73e/kn4j/isdp-docs#!method=get";
			const method = "get";
			const header = {};
			const params = {};
			const formData = new FormData();
			// formData.append('param1', 'value1');
			const xhr = new XMLHttpRequest();
			xhr.open(method, formatUrl());
			for (let key in header) xhr.setRequestHeader(key, header[key]);
			xhr.onreadystatechange = function() {
				if (xhr.readyState === 4) {
					if (xhr.status === 200) {
						console.log(xhr.responseText);
						pre.innerHTML = JSON.stringify(JSON.parse(xhr.responseText), null, 2);
					}
				}
			};
			xhr.onerror = function (error) {
			    console.log(error)
			}
			xhr.send(formatSend());
			
			function formatSend() {
				const contentType = params["Content-Type".toLocaleLowerCase()] || params["Content-Type"] || "";
				if (contentType === 'application/x-www-form-urlencoded') return Object.keys(params).map(key => (`${key}=${params[key]}`)).join("&");
		
				if (method.toLocaleLowerCase() === "post") return formData;
				return null;
			}
			function formatUrl() {
				let paramsArr = Object.keys(params)
				return paramsArr.length > 0 ? `${url}?` + paramsArr.map(key => (`${key}=${params[key]}`)).join("&") : url
			}
		</script>
	</body>
</html>